<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="选择委托单位" prop="customerId">
        <el-select
          v-model="form.customerId"
          placeholder="选择委托单位"
          style="width: 240px"
          @change="clearBuildList"
        >
          <el-option
            v-for="i in customerList"
            :key="i.userId"
            :label="i.nickName"
            :value="i.userId"
          />
        </el-select>
        <el-button type="primary" @click="getBuilds">确认</el-button>
      </el-form-item>
      <el-form-item label="选择建筑" prop="allBuild">
        <el-checkbox-group v-model="allBuild">
          <el-checkbox
            v-for="item in buildList"
            :label="item.id"
            :key="item.id"
            style="width: 25%"
          >
            {{ item.name }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item
        label="系统标准"
        v-if="form.projectSaveDTO"
        prop="projectSaveDTO.executionStandards"
      >
        <el-radio
          v-model="form.projectSaveDTO.executionStandards"
          v-for="item in dictList"
          :label="item.dictValue"
          :key="item.dictValue"
          >{{ item.dictLabel }}
        </el-radio>
      </el-form-item>
      <el-form-item label="选择系统">
        <el-checkbox-group v-model="allSystem" v-if="selectDict.length > 0">
          <el-checkbox
            v-for="item in selectDict"
            :label="item.id"
            :key="item.id"
            style="width: 25%"
          >
            {{ item.name }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <div>
      <el-button
        type="primary"
        style="margin-top: 12px"
        @click="changeOptions"
        v-if="form.status < 5 || form.status == 10"
      >
        下一步
      </el-button>
    </div>
  </div>
</template>
<script>
import { getUserList, getBuildList, getDevList } from "@/api/contract/contractOrg";
import { getDicts as getDicts } from "@/api/system/dict/data";
export default {
  props: {
    dataForm: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      form: {
        projectSaveDTO: {
          buildingId: "",
          executionStandards: "",
        },
        buildingSystemJsonStr: {
          buildingVos: [],
          systemVos: [],
        },
        status: 1,
      },
      // 表单校验
      rules: {
        customerId: [
          {
            required: true,
            message: "委托单位不能为空",
            trigger: ["change", "blur", "click"],
          },
        ],
        "projectSaveDTO.executionStandards": [
          { required: true, message: "请选择系统标准", trigger: "click" },
        ],
      },
      customerList: [], // 委托单位列表
      buildList: [], // 建筑列表
      dictList: [], // 系统标准
      selectDict: [],

      allBuild: [],
      allSystem: [],
    };
  },
  created() {
    this.getList();
    this.getSystemList();
    this.initForm();
  },
  methods: {
    // 初始化表单
    initForm() {
      if (Object.keys(this.dataForm).length !== 0 && this.dataForm.id !== 0) {
        this.form = { ...this.form, ...this.dataForm };
        this.getBuilds();
        if (
          this.form.buildingSystemJsonStr &&
          Object.keys(this.form.buildingSystemJsonStr).length !== 0
        ) {
          this.jsonCheck(this.form);
          this.form.buildingSystemJsonStr = JSON.parse(this.form.buildingSystemJsonStr);
        }
      }
    },
    getList() {
      this.loading = true;
      getUserList("02").then((res) => {
        this.customerList = res.data;
        this.loading = false;
      });
      // 查询系统标准
      getDicts("sys_check_standard").then((res) => {
        this.dictList = res.data;
      });
    },
    // 查询系统列表
    getSystemList() {
      getDevList({
        pageNum: 1,
        pageSize: 50,
      }).then((res) => {
        this.selectDict = res.rows;
      });
    },
    clearBuildList() {
      this.allBuild = [];
      this.buildList = [];
    },
    // 处理多选
    jsonCheck(list) {
      let jsonStrList = JSON.parse(list.buildingSystemJsonStr);
      for (let i = 0; i < jsonStrList.buildingVos.length; i++) {
        this.allBuild.push(jsonStrList.buildingVos[i].buildingId);
      }
      for (let i = 0; i < jsonStrList.systemVos.length; i++) {
        this.allSystem.push(jsonStrList.systemVos[i].systemId - 0);
      }
    },
    // 获取建筑列表
    getBuilds() {
      getBuildList({ customerId: this.form.customerId }).then((res) => {
        this.buildList = res.rows;
      });
    },
    //判定
    getTypeOfAll() {
      if (typeof this.form.projectSaveDTO.operators === "string") {
        this.form.projectSaveDTO.operators = this.form.projectSaveDTO.operators;
      } else if (Array.isArray(this.form.projectSaveDTO.operators)) {
        this.form.projectSaveDTO.operators = this.form.projectSaveDTO.operators.join(",");
      } else {
        // 可能需要设置一个默认值
        this.form.projectSaveDTO.operators = "";
      }
    },
    changeOptions() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.allBuild.length < 1) {
            this.$modal.msgWarning('建筑不能为空');
          } else if (this.allSystem.length < 1) {
            this.$modal.msgWarning('系统不能为空');
          } else {
            let buildCheck = [];
            let systemCheck = [];
            for (let i = 0; i < this.allBuild.length; i++) {
              for (let j = 0; j < this.buildList.length; j++) {
                if (this.allBuild[i] == this.buildList[j].id) {
                  buildCheck.push({
                    buildingId: this.buildList[j].id,
                    buildingName: this.buildList[j].name,
                  });
                }
              }
            }
            for (let i = 0; i < this.allSystem.length; i++) {
              for (let j = 0; j < this.selectDict.length; j++) {
                if (this.allSystem[i] == this.selectDict[j].id) {
                  systemCheck.push({
                    systemId: this.selectDict[j].id,
                    systemName: this.selectDict[j].name,
                  });
                }
              }
            }
            for (let i = 0; i < this.customerList.length; i++) {
              if (this.form.customerId == this.customerList[i].userId) {
                this.form.listName = this.customerList[i].nickName;
              }
            }
            this.form.buildingSystemJsonStr.buildingVos = buildCheck;
            this.form.buildingSystemJsonStr.systemVos = systemCheck;
            this.form.status = this.form.status > 1 ? this.form.status : 2;
            this.form.buildingSystemJsonStr = JSON.stringify(
              this.form.buildingSystemJsonStr
            );
            if (this.form.projectSaveDTO) {
              if (this.form.projectSaveDTO.operators) {
                this.getTypeOfAll();
              }
            }
            this.$nextTick(() => {
              this.$emit("refreshInfo", this.form);
            });
          }
        }
      });
    },
  },
};
</script>
